<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="公告标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticeName">
              <a-input v-model="model.noticeName" placeholder="请输入公告标题"></a-input> 
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="公告内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticeContent">
              <j-editor v-model="model.noticeContent" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-model="model.attachmentCode"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-row>
      <a-col :span="24">
        <a-form-model-item label="关联标段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="exSectionIds">
          <j-checkbox v-model="model.exSectionIds" :options="exSectionOptions" />
        </a-form-model-item>
      </a-col>
    </a-row>
    <a-row>
       <a-col :span="3">
       </a-col>
      <a-col :span="18">
        <ex-operation-record
          ref="exOperationRecord"
          :module="'exTenderAnnQuaInqueryAnn'"
          :entryId="model.id"
        ></ex-operation-record>
      </a-col>
    </a-row>
  </a-spin>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JCheckbox from '@/components/jeecg/JCheckbox'
import ExOperationRecord from '@/views/exOperationRecord/Index'

export default {
  name: 'ExTenderAnnQuaInqueryAnnForm',
  components: {
    JFormContainer,
    JCheckbox,
    ExOperationRecord,
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: { exSectionIds: '' },
      validatorRules: {
        tenderProjectCode: [{ required: true, message: '请输入招标项目编号!' }],
        unifiedDealCodes: [{ required: true, message: '请输入统一交易标识码!' }],
        superviseDeptName: [{ required: true, message: '请输入监督部门名称!' }],
        tenderCode: [{ required: true, message: '请输入招标编号!' }],
        noticeName: [{ required: true, message: '请输入公告标题!' }],
        noticeContent: [{ required: true, message: '请输入公告内容!' }],
        noticeSendTime: [{ required: true, message: '请输入公告发布时间!' }],
        noticeMedia: [{ required: true, message: '请输入公告发布媒体!' }],
        url: [{ required: true, message: '请输入公告源url!' }],
        noticeNature: [{ required: true, message: '请输入公告性质!' }],
        bulletinType: [{ required: true, message: '请输入公告类型!' }],
        docGetStartTime: [{ required: true, message: '请输入招标文件/资格预审文件获取时间!' }],
        docGetEndTime: [{ required: true, message: '请输入招标文件/资格预审文件获取截止时间!' }],
        tenderDocGetMethod: [{ required: true, message: '请输入招标文件/资格预审文件获取方法!' }],
        bidDocReferEndTime: [{ required: true, message: '请输入投标文件/资格预审申请文件递交截止时间!' }],
        bidOpenTime: [{ required: true, message: '请输入开标时间!' }],
        docPrice: [{ required: true, message: '请输入文件售价!' }],
        docPriceUnit: [{ required: true, message: '请输入文件售价单位!' }],
        bidDocReferMethod: [{ required: true, message: '请输入投标文件/资格预审申请文件递交方法!' }],
        syndicatedFlag: [{ required: true, message: '请输入是否允许联合体投标!' }],
        isPostpone: [{ required: true, message: '请输入是否延期开标!' }],
        tenderCodeType: [{ required: true, message: '请输入招标人代码类型!' }],
        tendererName: [{ required: true, message: '请输入招标人名称!' }],
        bulletinDuty: [{ required: true, message: '请输入公告发布责任人!' }],
        bulletinDutyCodeType: [{ required: true, message: '请输入交易平台验证责任人!' }],
        attachmentCode: [{ required: true, message: '请输入附件关联标识号!' }],
        platformCode: [{ required: true, message: '请上传附件!' }],
        noticeEndTime: [{ required: true, message: '请输入公告截止时间!' }],
        exSectionIds: [{ required: true, message: '请选择关联标段!' }],
      },
      url: {
        add: '/exTenderAnnQuaInqueryAnn/exTenderAnnQuaInqueryAnn/add',
        edit: '/exTenderAnnQuaInqueryAnn/exTenderAnnQuaInqueryAnn/edit',
        queryById: '/exTenderAnnQuaInqueryAnn/exTenderAnnQuaInqueryAnn/queryById',
        exGcNoticesection: {
          list: '/exTenderAnnQuaInqueryAnn/exTenderAnnQuaInqueryAnn/queryExGcNoticesectionByMainId',
        },
        exSection: {
          list: '/exProject/exProject/queryExSectionByMainId',
        },
      },
      exSectionDataSource: [],
      exSectionOptions: [],
      confirmLoading: false,
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    projectInfo: {
      default: {},
    },
    bulletinType: {
      default: {},
    },
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    this.model.tenderProjectCode = this.projectInfo.projectCode
    this.model.projectId = this.projectInfo.projectId

    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      record.exSectionIds = ''
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.model.projectId) {
        this.getExSectionList(this.model.projectId)
      }
    },
    getExSectionList(projectId) {
      //获取标段列表
      var that = this
      getAction(this.url.exSection.list, { id: projectId })
        .then((res) => {
          if (res.success) {
            that.exSectionOptions = []
            that.exSectionDataSource = res.result
            that.exSectionDataSource.forEach((row) => {
              that.exSectionOptions.push({ label: row.bidSectionName, value: row.id })
            })
            if (that.model.id) {
              that.lodaExGcNoticesection(that.model.id)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    lodaExGcNoticesection(id) {
      //获取保存的标段关系
      var that = this
      getAction(this.url.exGcNoticesection.list, { id: id })
        .then((res) => {
          if (res.success) {
            that.model.exSectionIds = ''
            var ids = []
            res.result.forEach((element) => {
              ids.push(element.bidSectionId)
            })
            that.model.exSectionIds = ids.join(',')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          that.model.exGcNoticesectionList = []
          for (let i = 0; i < that.model.exSectionIds.split(',').length; i++) {
            const element = that.model.exSectionIds.split(',')[i]
            that.model.exGcNoticesectionList.push({ bidSectionId: element })
          }
          that.model.bulletinType = this.bulletinType;
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

<style scoped>
</style>